/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author A7med Kotb
 */
public class MainScheduleTask extends org.apache.struts.action.ActionForm {

    private String taskName = "";
    private String schedule = "";
    private String modifier = "";
    private String startDate = "";
    private String startTime = "";
    private String[] day;
    private Date startDated;
    private String index = "";
    private String id;
    private String taskType = "";
    private String[] month;
    private boolean isenable;
    private boolean isrunning;
    
    public boolean getIsenable() {
        return isenable;
    }

    public void setIsenable(boolean isenable) {
        this.isenable = isenable;
    }

    public boolean getIsrunning() {
        return isrunning;
    }

    public void setIsrunning(boolean isrunning) {
        this.isrunning = isrunning;
    }
    
    public String[] getMonth() {
        return month;
    }

    public void setMonth(String[] month) {
        this.month = month;
    }

    public String getTaskType() {
        return taskType;
    }

    public void setTaskType(String taskType) {
        this.taskType = taskType;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public Date getStartDated() {
        Date date = null;
        try {
            String str_date = this.getStartDate();
            DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
            java.util.Date d = (java.util.Date) formatter.parse(str_date);
            SimpleDateFormat DBFormat = new SimpleDateFormat("yyyy-MM-dd");
            String DBDate = DBFormat.format(d);
            java.util.Date DBD = (java.util.Date) DBFormat.parse(DBDate);
            date = new Date(DBD.getTime());
            System.out.println("Date is " + date);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return date;
    }

    public void setStartDated(Date startDated) {
        this.startDated = startDated;
    }

    public String getTaskName() {
        return taskName.trim();
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public String[] getDay() {
        return day;
    }

    public void setDay(String[] day) {
        this.day = day;
    }

    public String getModifier() {
        return modifier;
    }

    public void setModifier(String modifier) {
        this.modifier = modifier;
    }

    public String getSchedule() {
        return schedule;
    }

    public void setSchedule(String schedule) {
        this.schedule = schedule;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    /**
     *
     */
    public MainScheduleTask() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getIndex().equalsIgnoreCase("notfound")) {
            try {
                if (getTaskName() == null || getTaskName().length() < 1) {
                    errors.add("EmptyTaskName", new ActionMessage("error.taskname"));
                }
                if (getSchedule().equals("0")) {
                    errors.add("EmptySchedule", new ActionMessage("error.schedule"));
                }
                if (getStartDate() == null || getStartDate().length() < 1) {
                    errors.add("EmptyStartDate", new ActionMessage("error.startdate"));
                } else {
                    Calendar cal = Calendar.getInstance();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    String dateNow = formatter.format(cal.getTime());
                    java.util.Date dn = formatter.parse(dateNow);
                    java.util.Date du = new java.util.Date(getStartDated().getTime());
                    System.out.println("DN-----------------" + dn);
                    System.out.println("DU------------------" + du);
                    if (!du.equals(dn)) {
                        if (du.before(dn)) {
                            errors.add("EmptyStartDate", new ActionMessage("error.startdate"));
                        }
                    }
                }
                if (getStartTime() == null || getStartTime().length() < 1) {
                    errors.add("EmptyStartTime", new ActionMessage("error.starttime"));
                }
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
        }
        if (getIndex().equalsIgnoreCase("found")) {
            if (getTaskName() == null || getTaskName().length() < 1) {
                errors.add("EmptyTaskName", new ActionMessage("error.taskname"));
            }
            if (getSchedule().equals("0")) {
                errors.add("EmptySchedule", new ActionMessage("error.schedule"));
            }
            if (getStartDate() == null || getStartDate().length() < 1) {
                errors.add("EmptyStartDate", new ActionMessage("error.startdate"));
            }
            if (getStartTime() == null || getStartTime().length() < 1) {
                errors.add("EmptyStartTime", new ActionMessage("error.starttime"));
            }
        }
        return errors;
    }
}
